package com.example.amazlinglistview;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.example.amazlinglistview.adapter.MyAdapter;
import com.example.amazlinglistview.domain.Constants;
import com.example.amazlinglistview.domain.Dealers;
import com.example.amazlinglistview.domain.SeriesInfo;
import com.example.amazlinglistview.task.CarSeriesDealersTask;
import com.example.amazlinglistview.task.CarSeriesTask;
import com.example.amazlinglistview.task.MyHandler;
import com.example.amazlinglistview.widget.AmazingListView;

public class MainActivity extends Activity implements OnCheckedChangeListener {

	private static final String TAG = "MainActivity";
	public final String CARSERIES_URL = "http://newcar.xcar.com.cn/auto/index.php?r=service/IphoneApi/GetSeriesInfoByPserId&seriesId=553";
	public final String DEALERS_URL = "http://newcar.xcar.com.cn/auto/index.php?r=service/IphoneApi/GetDealersBySeriesId&seriesId=553&city=475&type=1&province=";

	LayoutInflater mInflater;
	public AmazingListView mListView;
	public ProgressBar mProgressbar;

	public SeriesInfo mSeriesInfo;
	public Dealers mDealers;
	public MyAdapter mAdapter;
	public MyHandler mHandler;
	public View mHeaderView;
	private RadioGroup mRgType, mRgSort;
	private RadioButton mRbtn4S, mRbtnCompre, mRbtnDistance, mRbtnHot,
			mRbtnPrice;

	public int mDealersState = -1;
	public int mSortState = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mSeriesInfo = new SeriesInfo();
		mDealers = new Dealers();
		mInflater = getLayoutInflater();
		mHandler = new MyHandler(this);
		initView();
	}

	// 初始化
	private void initView() {
		mListView = (AmazingListView) findViewById(R.id.alistview_main);
		mProgressbar = (ProgressBar) findViewById(R.id.loading_bar_main);
		mHeaderView = findViewById(R.id.header_dealers_main);
		mRgType = (RadioGroup) mHeaderView.findViewById(R.id.rg_types_header);
		mRgSort = (RadioGroup) mHeaderView.findViewById(R.id.rg_sort_header);
		mRbtn4S = (RadioButton) mHeaderView.findViewById(R.id.rb_4s_header);
		mRbtnCompre = (RadioButton) mHeaderView
				.findViewById(R.id.rb_compre_header);
		mRbtnDistance = (RadioButton) mHeaderView
				.findViewById(R.id.rb_distance_header);
		mRbtnHot = (RadioButton) mHeaderView.findViewById(R.id.rb_hot_header);
		mRbtnPrice = (RadioButton) mHeaderView
				.findViewById(R.id.rb_price_header);

		mRgType.setOnCheckedChangeListener(this);
		mRgSort.setOnCheckedChangeListener(this);

		// 默认选中4S店和按距离排序
		mDealersState = Constants.DEALER_STATE_4S;
		mSortState = Constants.SORT_STATE_DISTANCE;
		setRbtnsState();
	}

	@Override
	protected void onStart() {
		super.onStart();
		// 加载数据
		initData();
	}

	/** 加载数据 */
	public void initData() {
		if (MyHandler.DATA_STATE == 0) {// 还没有加载过数据，则加载车型信息
			Log.i(TAG, "---开始加载信息---");
			new CarSeriesTask(this).execute(CARSERIES_URL);
		} else if (MyHandler.DATA_STATE == MyHandler.SERIES_READY) {
			// 车型信息已经加载完毕,开始加载经销商信息
			Log.i(TAG, "---车型信息加载完毕，开始加载经销商信息---");
			new CarSeriesDealersTask(this).execute(DEALERS_URL);
		} else if (MyHandler.DATA_STATE == MyHandler.DEALERS_READY) {
			// 经销商信息加载完毕,则设置Adapter
			Log.i(TAG, "---经销商信息加载完毕，设置适配器---");
			mProgressbar.setVisibility(View.GONE);
			mAdapter = new MyAdapter(this, mSeriesInfo, mDealers);
			mListView.setAdapter(mAdapter);
			View pinnerView = mInflater.inflate(R.layout.car_series_section,
					mListView, false);
			mListView.setPinnedHeaderView(pinnerView);
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.rb_4s_header:
			if (mDealersState != Constants.DEALER_STATE_4S) {
				mDealersState = Constants.DEALER_STATE_4S;
				mHandler.sendEmptyMessage(Constants.DEALER_STATE_4S);
			}
			break;
		case R.id.rb_compre_header:
			if (mDealersState != Constants.DEALER_STATE_COMPRE) {
				mDealersState = Constants.DEALER_STATE_COMPRE;
				mHandler.sendEmptyMessage(Constants.DEALER_STATE_COMPRE);
			}

			break;
		case R.id.rb_distance_header:
			if (mSortState != Constants.SORT_STATE_DISTANCE) {
				mSortState = Constants.SORT_STATE_DISTANCE;
				mHandler.sendEmptyMessage(Constants.SORT_STATE_DISTANCE);
			}

			break;
		case R.id.rb_hot_header:
			if (mSortState != Constants.SORT_STATE_HOT) {
				mSortState = Constants.SORT_STATE_HOT;
				mHandler.sendEmptyMessage(Constants.SORT_STATE_HOT);
			}

			break;
		case R.id.rb_price_header:
			if (mSortState != Constants.SORT_STATE_PRICE) {
				mSortState = Constants.SORT_STATE_PRICE;
				mHandler.sendEmptyMessage(Constants.SORT_STATE_PRICE);
			}
			break;
		}
		if (mAdapter != null) {
			mAdapter.setRbtnsState();
		}
	}

	public void setRbtnsState() {
		if (mDealersState == Constants.DEALER_STATE_4S) {
			mRbtn4S.setChecked(true);
		} else if (mDealersState == Constants.DEALER_STATE_COMPRE) {
			mRbtnCompre.setChecked(true);
		}

		if (mSortState == Constants.SORT_STATE_DISTANCE) {
			mRbtnDistance.setChecked(true);
		} else if (mSortState == Constants.SORT_STATE_HOT) {
			mRbtnHot.setChecked(true);
		} else if (mSortState == Constants.SORT_STATE_PRICE) {
			mRbtnPrice.setChecked(true);
		}

	}
}
